package com.project.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.project.entity.Contact;
import com.project.util.DBHelp;
import com.project.util.RowMapper;

public class ContactDao {
	DBHelp<Contact> db=new DBHelp<Contact>();
	public void save(Contact c) {
		String sql = "insert into t_contact(id,`name`,company,moblie,tel,email,address,web,weibo,cont,projectid,createtime)VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getName(),c.getCompany(),c.getMoblie(),c.getTel(),c.getEmail(),c.getAddress(),c.getWeb(),c.getWeibo(),c.getCont(),c.getProjectid(),c.getCreatetime());
	}

	public List<Contact> findAllContactByProjectid(String projectid) {
		/*StringBuilder sql = new StringBuilder();
		sql.append("SELECT id,`name`,company,moblie,tel,email,address,web,weibo,cont,projectid,createtime,t_project.name as 'projectname',t_project.id as 'projectid',t_poject.cont as 'projectcont',t_project.createtime as 'projectcreatetime' FROM t_contact ");
		sql.append("left join t_contact on t_project.id = t_contact.projectid ");
		sql.append("where t_task.id = ?  order by createtime desc");
		return db.executeQueryForList(sql.toString(), new contactRowMapper(), projectid);*/
		
		String sql="select * from t_contact where projectid=?";
		return db.executeQueryForList(sql, new contactRowMapper(), projectid);
		
	}

	
	private class contactRowMapper implements RowMapper<Contact>{

		public Contact mapRow(ResultSet rs) throws SQLException {
			
			Contact c= new Contact();
			c.setId(rs.getString("id"));
			c.setCompany(rs.getString("company"));
			c.setMoblie(rs.getString("moblie"));
			c.setTel(rs.getString("tel"));
			c.setEmail(rs.getString("email"));
			c.setWeb(rs.getString("web"));
			c.setWeibo(rs.getString("weibo"));
			c.setProjectid(rs.getString("projectid"));
			c.setAddress(rs.getString("address"));
			c.setCreatetime(rs.getString("createtime"));
			c.setName(rs.getString("name"));
			c.setCont(rs.getString("cont"));
			
			
			/*Project project =new Project();
			project.setCont(rs.getString("cont"));
			project.setCreatetime(rs.getString("createtime"));
			project.setId(rs.getString("id"));
			project.setName(rs.getString("name"));
			
			c.setProject(project);*/
			return c;
		}
		
	}


	public void update(Contact c) {
		String sql="update t_contact set name=?,company=?,moblie=?,tel=?,email=?,web=?,weibo=?,projectid=?,address=?,createtime=?,cont=? where id=?";
		db.executeSQL(sql,c.getName(),c.getCompany(),c.getMoblie(),c.getTel(),c.getEmail(),c.getWeb(),c.getWeibo(),c.getProjectid(),c.getAddress(),c.getCreatetime(),c.getCont(),c.getId());
	}

	public void delContactById(String cid) {
		String sql="delete from t_contact where id=?";
		db.executeSQL(sql, cid);
	}

	public Contact findContactById(String id) {
		String sql="select * from t_contact where id  = ?"; 
		return db.executeQueryForObject(sql, new contactRowMapper(), id);
				
	
	}
	
	
}
